package com.oo.app.ui.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.oo.app.R;
import com.oo.app.base.activities.BaseActivity;
import com.oo.app.ui.bean.UserInfo;
import com.oo.app.ui.main.activity.boy.HomeBoyActivity;
import com.oo.app.utils.AccountHelper;
import com.oo.app.widget.toast.Prompt;
import com.oo.app.widget.toast.SnackbarToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @创建者 赵聪
 * @描述 绑定手机号
 */

public class BindingPhoneActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.sp_num)
    Spinner sp_num;

    @BindView(R.id.et_phone)
    EditText et_phone;

    @BindView(R.id.iv_phone_del)
    ImageView iv_phone_del;

    @BindView(R.id.et_code)
    EditText et_code;

    @BindView(R.id.tv_code)
    TextView tv_code;

    @BindView(R.id.ll_code_time)
    LinearLayout ll_code_time;

    @BindView(R.id.tv_second)
    TextView tv_second;

    @BindView(R.id.iv_code_del)
    ImageView iv_code_del;

    @BindView(R.id.btn_the_next)
    Button btn_the_next;

    private TimeCount time;// 计时工具
    public boolean isNext = false;

    public static void show(Context context) {
        Intent intent = new Intent(context, BindingPhoneActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_binding_phone;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        tvTitle.setText("绑定手机号");
        // 倒计时60秒
        time = new TimeCount(59000, 1000);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                iv_phone_del.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                btn_the_next.setBackgroundResource(R.mipmap.login_btn_selected);
                isNext = true;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        sp_num.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                String[] languages = getResources().getStringArray(R.array.arrayPhoneHead);
                Toast.makeText(BindingPhoneActivity.this, "你点击的是:"+languages[pos], Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
    }

    @OnClick({R.id.iv_back,R.id.btn_the_next,R.id.tv_code,R.id.iv_phone_del,R.id.iv_code_del})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_the_next: //下一步
                AlipayAccountActivity.show(BindingPhoneActivity.this);
                if (isNext){

                }else {
                    SnackbarToast.make(this, "ERROR", 3000l).setPromptThemBackground(Prompt.ERROR).show();
                }
                break;
            case R.id.tv_code:
                String phone = et_phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)){
                    SnackbarToast.make(this, "请输入手机号码！", 3000l)
                            .setPromptThemBackground(Prompt.ERROR)
                            .show();
                    return;
        }
        tv_code.setVisibility(View.GONE);
        et_code.setVisibility(View.VISIBLE);
        ll_code_time.setVisibility(View.VISIBLE);
        time.start();

                break;
            case R.id.iv_phone_del:
                et_phone.setText("");
                break;
            case R.id.iv_code_del:
                time.start();
                ll_code_time.setVisibility(View.VISIBLE);
                iv_code_del.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 短信验证时间倒计时
     *
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_second.setText( millisUntilFinished / 1000 +"");
        }

        @Override
        public void onFinish() {
            iv_code_del.setVisibility(View.VISIBLE);
            ll_code_time.setVisibility(View.GONE);
        }
    }

}
